﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace VTFC
{
    // ----------------------------------------------------------------------------
    /// <summary>
    /// Configuration manager class.
    /// </summary>
    /// <remarks>
    /// Read and write configuration settings.
    /// </remarks>
    // ----------------------------------------------------------------------------
    public class ConfigManager
    {
        // ----------------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        // ----------------------------------------------------------------------------
        static ConfigManager()
        {
        }//ctor

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Gets a config value in type string.
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>Value</returns>
        // ----------------------------------------------------------------------------
        public static string GetString( string key )
        {
            return ConfigurationManager.AppSettings[key];
        }//func GetString

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Gets a config value in type bool.
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>Value</returns>
        // ----------------------------------------------------------------------------
        public static bool GetBoolean(string key)
        {
            return Boolean.Parse(GetString(key));
        }//func GetBoolean

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Gets a config value in type int.
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>Value</returns>
        // ----------------------------------------------------------------------------
        public static int GetInt32(string key)
        {
            return Int32.Parse(GetString(key));
        }//func GetInt32

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Save appSettings changes, than refresh it.
        /// </summary>
        /// <param name="configvalues">Configuration settings in key-value collection.</param>
        // ----------------------------------------------------------------------------
        public static void SaveConfiguration(Dictionary<string, string> configvalues)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            foreach (KeyValuePair<string, string> value in configvalues)
            {
                config.AppSettings.Settings.Remove(value.Key);
                config.AppSettings.Settings.Add(value.Key, value.Value);
            }
            config.Save(ConfigurationSaveMode.Modified, true);
            ConfigurationManager.RefreshSection(Constant.VTFC_CONFIGURATION_APPSETTINGS);
        }//func SaveConfiguration
    }//class ConfigManager
}//namespace VTFC
